package com.hellowx.qydcdev.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hellowx.qydcdev.dao.AddressDao;
import com.hellowx.qydcdev.entity.Address;
import com.hellowx.qydcdev.exception.CustomException;
import com.hellowx.qydcdev.exception.CustomExceptionType;
import com.hellowx.qydcdev.service.AddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created on 2022/1/7.
 *
 * @author Hello WX
 */
@Service("addressService")
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressDao addressDao;

    @Override
    public List<Address> listAddressByCustomerId(Long customerId) {
        return new LambdaQueryChainWrapper<>(addressDao)
                .eq(Address::getCustomerId,customerId)
                .list();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveOrUpdateAddress(Address address) {
        Address one = new LambdaQueryChainWrapper<>(addressDao)
                .eq(Address::getId, address.getId())
                .one();
        int rows = 0;
        String msg = null;
        if (one==null){
            rows = addressDao.insert(address);
            msg = "保存成功!!";
        }else {
            rows = addressDao.updateById(address);
            msg = "修改成功!!";
        }
        if (rows>0){
            return msg;
        }
        throw new CustomException(CustomExceptionType.SYSTEM_EXCEPTION);
    }
}
